package com.mall.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.mall.common.exception.HttpStatusCode;
import com.mall.common.utils.Result;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author: 谢君臣
 * @Date: 2022/9/22-16:46
 * @version: 1.0
 * @function:
 */
@Configuration
public class GatewaySentinelConfig {
    public GatewaySentinelConfig() {
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                Result<Object> result = new Result<>().error(HttpStatusCode.TOO_MANY_REQUEST.getCode(),HttpStatusCode.TOO_MANY_REQUEST.getMsg());
                Mono<ServerResponse> body = ServerResponse.ok().body(Mono.just(result), Result.class);
                return body;
            }
        });
    }
}
